<?php
/**
 * OrderRepo.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-05-17 15:22:05
 * @modified   2023-05-17 15:22:05
 */

namespace Beike\Seller\Repositories;

use Beike\Models\Order;
use Beike\Services\StateMachineService;
use Illuminate\Database\Eloquent\Builder;

class OrderRepo extends \Beike\Repositories\OrderRepo
{
    /**
     * @param array $filters
     * @param $sellerId
     * @return Builder
     */
    public static function getListBuilder(array $filters = [], $sellerId = null): Builder
    {
        $builder = parent::getListBuilder($filters);
        if ($sellerId === null) {
            $sellerId = current_seller_user()->seller_id ?? 0;
        }
        if ($sellerId) {
            $builder->where('seller_id', $sellerId);
        }

        return $builder;
    }

    /**
     * @param $orderId
     * @return Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getSubOrderByOrderId($orderId)
    {
        return Order::query()->where('parent_id', $orderId)->get();
    }

    /**
     * @param $orderId
     * @return int
     */
    public static function hasSubOrder($orderId)
    {
        return self::getSubOrderByOrderId($orderId)->count();
    }

    /**
     * @param $sellerId
     * @return array
     */
    public static function getTotalOrdersByMonth($sellerId)
    {
        $order_data = [];

        for ($i = 1; $i <= date('t'); $i++) {
            $date = date('Y') . '-' . date('m') . '-' . $i;

            $order_data[date('j', strtotime($date))] = [
                'day'    => date('d', strtotime($date)),
                'amount' => 0,
                'count'  => 0,
            ];
        }

        $status = StateMachineService::getValidStatuses();
        $orders = \Beike\Seller\Models\Order::query()->where('seller_id', $sellerId)
            ->whereIn('status', $status)
            ->whereRaw("DATE(created_at) >= '" . date('Y') . '-' . date('m') . '-1' . "'")
            ->groupByRaw('DATE(created_at)')
            ->selectRaw('COUNT(*) AS total, SUM(total) AS amount, date(created_at) as created_at')
            ->get();

        foreach ($orders as $result) {
            $order_data[date('j', strtotime($result['created_at']))]['count']  = $result['total'];
            $order_data[date('j', strtotime($result['created_at']))]['amount'] = $result['amount'];
        }

        return $order_data;
    }

    /**
     * @param $sellerId
     * @return array
     */
    public static function getOrderChartData($sellerId): array
    {
        $order_data = self::getTotalOrdersByMonth($sellerId);

        $chart = [];
        foreach ($order_data as $item) {
            $chart['date'][]   = $item['day']; // 日期
            $chart['amount'][] = $item['amount']; //  订单金额
            $chart['count'][]  = $item['count']; // 订单数量
        }

        return $chart;
    }

    /**
     * @return int
     */
    public static function count(): int
    {
        return self::getListBuilder()->count();
    }
}
